# -*- coding:utf-8 -*-
import requests
import csv
import json
'''
主要功能：
1、连接mysql数据库
2、
'''
class foodQuantifiers():

    def foodquanty(self):

        # 初始化
        food_flag_init = 0
        food_error_init = 0
        food_fail_init = 0
        # 初始化登录接口
        login_url = 'https://api.icarbonx.com/oauth2/token?grant_type=password&sms_verify=true'
        login_header = {"Authorization": "Basic Y29tLm1ldW0uY29hY2guaXBob25lOjdmYTYyZmFmYzgxZTgwMzY="}
        login_data = {
            "username": "15012345678",
            "password": "888888",
            "appName": "health-buddy",
            "grant_type": "password",
            "sms_verify": "true"
        }
        # 登录请求接口
        r_login = requests.post(url=login_url, data=login_data, headers=login_header)

        # 读取csv文件
        with open('E:\\test\\HB\\foodQuanty.csv', 'r') as csvFile:
            reader = csv.reader(csvFile)
            print(type(reader))
            next(reader)
            for row in reader:
                print(row)
                print(type(row))
                print(row[0])
                print(type(row[0]))

                # 食物qa问答接口的请求参数msg
                food_msg = {"msg": row[0]}
                print(food_msg)

                # 获取登录的响应报文
                print(r_login.text)
                login_response = json.loads(r_login.text)
                # 保存登录的token信息
                access_token = login_response['access_token']
                food_headers = {"Authorization": "Bearer " + access_token}
                # 食物量词的url
                food_url = 'https://api.icarbonx.com/nlp/api/v1.0/nutrition_query'
                # qa问答模型接口请求
                r_food = requests.post(url=food_url, data=food_msg, headers=food_headers)
                # 获取响应报文
                print(r_food.text)
                # 转换响应结果为dict格式
                food_response = json.loads(r_food.text)
                print(food_response)
                print(type(food_response))

                # 判断响应结果是否为空,不为空，则获取food和name的名称
                if food_response:
                    print(food_response[0])
                    food_response_all = food_response[0]
                    food_quanty_food_name = food_response_all['food_name']
                    # 获取cal_name的值
                    food_quanty_food_id = food_response_all['food_id']
                    food_quanty_food_unit = food_response_all['food_unit']
                    food_quanty_food_grams = food_response_all['food_grams']
                    print("food的name：" + food_quanty_food_name)
                    print("食物id：%d" %food_quanty_food_id)
                    print("食物单位：" + food_quanty_food_unit)
                    print("食物重量：%d" %food_quanty_food_grams)

                    #int转换为str
                    food_quanty_food_id = str(food_quanty_food_id)
                    food_quanty_food_grams = str(food_quanty_food_grams)

                    food_quanty_data = ['food_name', 'grams', 'unit','id']

                    # with open('E:\\test\\HB\\food_quanty_all.csv','a+',encoding='utf-8-sig') as ff:
                    #
                    #     food_quanty_data[0] = food_quanty_food_name
                    #     food_quanty_data[1] = food_quanty_food_grams
                    #     food_quanty_data[2] = food_quanty_food_unit
                    #     food_quanty_data[3] = food_quanty_food_id
                    #     ff.write(','.join(food_quanty_data))
                    #     ff.write('\n')
                    #     ff.close()


        csvFile.close()




if __name__ == '__main__':
    fd = foodQuantifiers()
    fd.foodquanty()